package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理属性相关的请求
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "05.属性管理模块")
@Slf4j
@Validated
@RequestMapping("/attributes")
@RestController

public class AttributeController {

    @Autowired
    private IAttributeService attributeService;
    public AttributeController(){
        log.debug("创建控制器对象: AttributeController");
    }
    //http://localhost:9080/attributes/add-new?name=属性101&description=属性101的简介&sort=152
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add-new")
    //                       ↓↓↓↓↓↓以下是新添加的注解
    public JsonResult<Void>  addNew(@Valid AttributeAddNewDTO attributeAddNewDTO){
        log.debug("开始处理[添加属性]的请求,参数:{}",attributeAddNewDTO);
            attributeService.addNew(attributeAddNewDTO);
            log.debug("测试添加数据成功!");
            return JsonResult.ok();

    }

    // http://localhost:9080/attributes/9527/delete
    @ApiOperation("根据id删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除属性】的请求：id={}", id);
        attributeService.deleteById(id);
        return JsonResult.ok();
    }


//    //http://localhost:9080/attributes/9527/delete
//    @ApiOperation("根据id删除相册")
//    @ApiOperationSupport(order = 2)
//    @ApiImplicitParam(name = "id",value = "相册id",required = true,dataType = "long")
//    @PostMapping( "/{id:[0-9]+}/delete")
//    public JsonResult<Void> delete(@Range(min = 1,message = "删除相册失败，尝试删除的相册的ID无效！")
//                             @PathVariable Long id){
//        log.debug("开始处理[根据id删除相册]的请求,参数: {}", id);
//        albumService.delete(id);
//        return JsonResult.ok();
//    }
//

    // http://localhost:9080/attributes/9527/select
    @ApiOperation("根据id查询属性详情")
    @ApiOperationSupport(order = 419)
    @GetMapping("/{id:[0-9]+}/select")
    public JsonResult<AttributeStandardVO> getStandardById(@Range(min = 1,message = "根据id查询属性详情失败，尝试查询的属性的ID无效！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询属性详情】的请求，无参数");
        AttributeStandardVO queryResult = attributeService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9080/attributes
    @ApiOperation("查询属性列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<AttributeListItemVO>> list() {
        log.debug("开始处理【查询相册列表】的请求，无参数");
        List<AttributeListItemVO> list = attributeService.list();
        return JsonResult.ok(list);
    }
//
//    //http://localhost:9080/albums/9527/delete
//    @ApiOperation("[已过期]根据名称删除相册")
//    @ApiOperationSupport(order = 3)
//    @RequestMapping(value = "/{name:[a-z]+}/delete",method = RequestMethod.POST)
//    public String deletePos(@PathVariable String name){
//        String message = "尝试删除name值为[" + name + "]的相册";
//        log.debug(message);
//        return message;
//    }
//    //http://localhost:9080/albums/test/delete
//    @ApiOperation("[已过期]测试删除相册")
//    @ApiOperationSupport(order = 4)
//    @RequestMapping(value = "/test/delete",method = RequestMethod.POST)
//    public String deleteTest(){
//        String message = "尝试测试删除相册";
//        log.debug(message);
//        return message;
//    }

    //http://localhost:9080/attributes/9527/update
    @ApiOperation("根据id修改属性详情")
    @ApiOperationSupport(order = 3)
    @PostMapping( "/update")
    public JsonResult<Void> updateById(@Valid AttributeUpdateDTO attributeUpdateDTO){
        log.debug("开始处理[根据id修改属性详情]的请求,参数: {}", attributeUpdateDTO.getId());
        attributeService.updateById(attributeUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributes/list-by-template
    @ApiOperation("根据属性模板查询属性列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "templateId", value = "属性模板id",
            required = true, dataType = "long")
    @GetMapping("/list-by-template")
    public JsonResult<List<AttributeListItemVO>> listByTemplateId(Long templateId) {
        List<AttributeListItemVO> list = attributeService.listByTemplateId(templateId);
        return JsonResult.ok(list);
    }
}
